import Eosjs from "eosjs"
import ecc from 'eosjs-ecc'

let Eos = window.Eos;

/**
 * 初始化
 */
const init = (keyProvider, httpEndpoint) => {
    let config = {
        chainId: "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906",
        keyProvider: ['5J31NJnshp9iA1d3Bu2gAuwrUUJFAYw2FxHYG7kDctjUdSnphVy'],
        // httpEndpoint: 'http://mainnet.genereos.io',
        httpEndpoint: 'https://api.eoslaomao.com',
        // expireInSeconds: 60,
        broadcast: true,
        verbose: true, // API activity
        sign: true
    };
    // Constants.Eos = Eosjs(config);
    window.EosConfig = config;
    window.Eos = Eosjs(config);
    console.log('EosUtil init ' + window.Eos);
};

/**
 * 通过助记词生成私钥/公钥
 */
const seedPrivateKey = (seed, callback) => {
    ecc.seedPrivate(seed).then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil seedPrivateKey: ' + data);
    })
};

/**
 * 生成随机私钥
 */
export const randomPrivateKey = (callback) => {
    ecc.randomKey().then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil Private Key: ' + data);
    })
};

/**
 * 私钥生成公钥
 */
const privateToPublic = (privateKey, callback) => {
    ecc.privateToPublic(privateKey).then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil privateToPublic: ' + data);
    })
};

/**
 * 对数据进行签名
 */
const sign = (data, pk, callback) => {
    ecc.sign(data, pk).then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil sign: ' + data);
    })
};

/**
 * 转账
 */
const transfer = (contract, from, to, quantity, memo, pk, broadcast, callback) => {
    /*let options = {
        authorization: from+'@active',
        broadcast: broadcast,
        sign: true
    };
    window.eos.transfer(from, to, quantity, memo, options);*/
    //eos.transfer('alice', 'bob', '1.0000 SYS', '', options)


    /*const e = Eosjs({keyProvider:[pk]});
    eos.transfer(from, to, quantity, memo, true).then(tr => {
        console.log('EosUtil tr='+JSON.stringify(tr));
    })*/

    /*let config = {
        chainId: "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906",
        keyProvider: [pk],
        // httpEndpoint: 'http://mainnet.genereos.io',
        httpEndpoint: 'https://api.eoslaomao.com',
        // expireInSeconds: 60,
        broadcast: true,
        verbose: true, // API activity
        sign: true
    };*/
    // let config = {...window.EosConfig,keyProvider:[pk]};
    let config = window.EosConfig;
    config.keyProvider = [pk];
    console.log('EosUtil config=' + JSON.stringify(config));
    const eos = Eosjs(config);

    /*eos.getAccount('zuowen123123')
        .then(r => {
            console.log('EosUtil getAccount='+JSON.stringify(r));
        });*/

     let options = {
         authorization: from+'@active',
         broadcast: broadcast,
         sign: true
     };
     //const token = eos.contract(contract);
     eos.transfer(from, to, quantity, memo, options).then(tr=>{
         console.log('EosUtil tr='+JSON.stringify(tr));
     });

    /*eos.transaction(tr => {
        tr.delegatebw({
            from: 'eosio',
            receiver: 'zuowen123123',
            stake_net_quantity: '0.0100 EOS',
            stake_cpu_quantity: '0.0100 SYS',
            transfer: 0
        })
    })*/

};

/**
 * 创建账户
 */
const createAccount = (creator, createPrivateKey, newAccount, onwerPublicKey, activePublicKey, callback) => {
};

/**
 * 查询余额
 */
const balance = (contract, account, callback) => {
};


/**
 * 检查公钥
 */
const checkPublicKey = (publickey, callback) => {
    ecc.isValidPublic(publickey).then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil checkPublicKey: ' + data);
    })
};

/**
 * 检查私钥
 */
const checkPrivateKey = (privatekey, callback) => {
    ecc.isValidPrivate(privatekey).then(data => {
        if (callback) {
            callback(data)
        }
        console.log('EosUtil checkPrivateKey: ' + data);
    })
};

/**
 * 抵押
 */
const delegate = (pk, pay, recive, cpu, net, callback) => {
};

/**
 * 解除抵押
 */
const undelegate = (pk, from, recive, cpu, net, callback) => {
};

/**
 * 购买内存
 */
const buyram = (pk, pay, recive, count, callback) => {
};

/**
 * 卖内存
 */
const sellram = (pk, recive, bytes, callback) => {
};

/**
 * 投票
 */
const vote = (actor, voter, producers, privateKey, callback) => {
    Eos.transaction({
        actions: [
            {
                account: 'eosio',
                name: 'voteproducer',
                authorization: [{
                    actor: actor,
                    permission: 'active'
                }],
                data: {
                    voter: voter,
                    proxy: '',
                    producers: producers //["producer111j", "producer111p"]
                }
            }
        ]
    }, privateKey, (resp) => {
        if (resp) {
            console.log('vote ' + JSON.stringify(resp.data));
            if (callback) {
                callback(resp.data)
            }
        }
    });
};


const unvote = (actor, voter, producters, privateKey, callback) => {
    //撤票

};

const delegatebw = (actor, from, receiver, net, cpu, privateKey, callback) => {
    // 抵押
    Eos.transaction({
        actions: [
            {
                account: 'eosio',
                name: 'delegatebw',
                authorization: [{
                    actor: actor,
                    permission: 'active'
                }],
                data: {
                    from: from,
                    receiver: receiver,
                    stake_net_quantity: net,
                    stake_cpu_quantity: cpu,
                    transfer: 0
                }
            }
        ]
    }, privateKey, (resp) => {
        if (resp) {
            console.log('delegatebw ' + JSON.stringify(resp.data));
            if (callback) {
                callback(resp.data)
            }
        }
    });
};

const undelegatebw = (actor, from, receiver, net, cpu, privateKey, callback) => {
    // 赎回
    Eos.transaction({
        actions: [
            {
                account: 'eosio',
                name: 'undelegatebw',
                authorization: [{
                    actor: actor,
                    permission: 'active'
                }],
                data: {
                    from: from,
                    receiver: receiver,
                    stake_net_quantity: net,
                    stake_cpu_quantity: cpu,
                    transfer: 0
                }
            }
        ]
    }, privateKey, (resp) => {
        if (resp) {
            console.log('undelegatebw ' + JSON.stringify(resp.data));
            if (callback) {
                callback(resp.data)
            }
        }
    });
};

export default {
    init,
    seedPrivateKey,
    randomPrivateKey,
    privateToPublic,
    sign,
    transfer,
    createAccount,
    balance,
    checkPublicKey,
    checkPrivateKey,
    delegate,
    undelegate,
    buyram,
    sellram,
    vote,
    unvote,
    delegatebw,
    undelegatebw
}